package es.uc3m.setichat.android;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.AsyncTask;
import es.uc3m.setichat.clases.SeTIChatMessageChat;

public class SeTIChatMessageController {
	private Context context;
	
	public SeTIChatMessageController(Context context){
		this.context = context;
	}
	
	public void insertMessage(String idContact,int origin,String message,String data){
		//origin 1 => el usuario de la aplicacion envia el mensaje.
		//origin 0 => el usuario recibe el mensaje.
		
		insertMessage(idContact,origin,message,System.currentTimeMillis(),data);
		
	}
	
	public void insertMessage(String idContact,int origin,String message,long time, String data){
		AsyncTask<String,Void, Void> insertChatMessageSQL = new AsyncTask<String,Void, Void>(){
			
    		@Override
    		protected Void doInBackground(String... args) {
    			SeTIChatSQLiteHelper setiDB = new SeTIChatSQLiteHelper(context, "BBDDSetichat", null, SeTIChatSQLiteHelper.SQL_VERSION); 
    	        SQLiteDatabase db = setiDB.getWritableDatabase();
    	        if(db!=null){
    	        		SQLiteStatement stmt = db.compileStatement("INSERT INTO messages (idContact,origin,message,time,data) VALUES(?,?,?,?,?)");
    	        		stmt.bindString(1, args[0]);
    	        		stmt.bindString(2, args[1]);
    	        		stmt.bindString(3, args[2]);
    	        		stmt.bindString(4, args[3]);
    	        		stmt.bindString(5, args[4]);
    	        		stmt.execute();
    	        }
    	        db.close();
    			setiDB.close();

            	return null;
    		}
    		
    		
    	};
		
    	insertChatMessageSQL.execute(idContact,String.valueOf(origin),message,String.valueOf(time),data);
	}
	
	public void insertLogin(String email,String mobil,String psw,String nick){
		SeTIChatSQLiteHelper setiDB = new SeTIChatSQLiteHelper(context, "BBDDSetichat", null, SeTIChatSQLiteHelper.SQL_VERSION); 
	    SQLiteDatabase db = setiDB.getWritableDatabase();
	    if(db!=null){
	    		SQLiteStatement stmt = db.compileStatement("INSERT INTO login (mobil,email,psw,nick) VALUES(?,?,?,?)");
	    		stmt.bindString(1, mobil);
	    		stmt.bindString(2, email);
	    		stmt.bindString(3, psw);
	    		stmt.bindString(4, nick);
	    		stmt.execute();
	    }
	    db.close();
		setiDB.close();
	}
	
	public ArrayList<SeTIChatMessageChat> getMessagesFromContact(String idContact){
		ArrayList<SeTIChatMessageChat> messages = new ArrayList<SeTIChatMessageChat>();
		
		SeTIChatSQLiteHelper setiDB = new SeTIChatSQLiteHelper(context, "BBDDSetichat", null, SeTIChatSQLiteHelper.SQL_VERSION);
		
		SQLiteDatabase db = setiDB.getReadableDatabase();
        Cursor c = db.query("messages",new String[]{"idContact","origin","message","time","data"},"idContact='"+idContact+"'", null,null,null,"time ASC");
        
    	c.moveToFirst();
    	while(!c.isAfterLast()){
    		SeTIChatMessageChat aux = new SeTIChatMessageChat(c.getString(2), c.getString(1),c.getString(3), c.getString(0),c.getString(4));
    		messages.add(aux);
    		c.moveToNext();
    	}
        c.close();
        db.close();
        setiDB.close();
		
		return messages;
	}
	
	
}
